/**
 * @author: welling okey007@126.com okey00789@gmai.com
 * 
 */
/***
 * reconstructing sinafox 2011-1-14 
 * use Weifox to be main namespace
 * sinafox will change its name to weifox(微狐) once I get it done. 
 * opensource ,will support twitter ,sina, fanfou ,tencent ,digu,zuosha soon...
 * 
 * expect: a REST tool (receive and send) 
 */
var Weifox={};
Weifox={
	version:'0.08',
	///////////////////////////////////////
	//some frequently used functions and variables  
	///////////////////////////////////////
	$:function(id){
		document.getElementById(id);
	},
	extend:function(base,newObj){
		var obj={};
		for(var x in base){
			obj[x]=base[x];
		}
		for(var x in newObj){
			obj[x]=newObj[x];
		}
		return obj;
	},
	/**
	 * run when firefox start
	 * Weifox的必要组件初始化,不能有太大的依赖性,如需几个类共同合作的初始化,
	 * 需放在delayInit();
	 */
	init:function(){
		//动态装载各个模块脚本.
		var loadService =Components.classes["@mozilla.org/moz/jssubscript-loader;1"].getService(Components.interfaces.mozIJSSubScriptLoader);
		var chrome="chrome://weifox/content";
		loadService.loadSubScript(chrome+"/util.js",null,"UTF-8");
		
		loadService.loadSubScript(chrome+"/net.js",null,"UTF-8");
		loadService.loadSubScript(chrome+"/browser.js",null,"UTF-8");
		loadService.loadSubScript(chrome+"/data.js",null,"UTF-8");
		loadService.loadSubScript(chrome+"/main.js",null,"UTF-8");
		
		loadService.loadSubScript(chrome+"/debug.js",null,"UTF-8");
		/***
		 * load apps' js 
		 */
		loadService.loadSubScript(chrome+"/apps/sina/sina.js",null,"UTF-8");
		loadService.loadSubScript(chrome+"/apps/fanfou/fanfou.js",null,"UTF-8");
		loadService.loadSubScript(chrome+"/apps/tencent/tencent.js",null,"UTF-8");
		loadService.loadSubScript(chrome+"/apps/netease/netease.js",null,"UTF-8");
		
		loadService.loadSubScript(chrome+"/ui.js");
		//xbl :使得Panel 上的XBL元素初始化 
		var main=Weifox.main;
		main.activateXBL();		
		
		window.setTimeout(Weifox.delayInit,5000);
	},
	unload:function(){
		var data=Weifox.data;
		//Weifox.debug.showObj(data.users[0]);
		data.saveUser();
		
	},
	/**
	 * 程序入口
	 */
	delayInit:function(){
//		try {
		var w=Weifox;
		w.ui.init();
		var main = w.main;
		//初始化用户
		w.data.loadUser();
		//初始化网络
		w.browser.init();
		w.data.SQLite.init();
		
		var apps=w.apps;
		for(var i in apps){
			if(apps[i].init){
				apps[i].init();
			}
		}
		//最后启动更新
		w.main.update();
		
		window.setInterval(w.main.update,30000);
//		}catch(e){alert('E:\n'+e);}
	},
	log:function(str){
	  Components.classes['@mozilla.org/consoleservice;1']
	            .getService(Components.interfaces.nsIConsoleService)
	            .logStringMessage(str);
	},	
	/////////////////////////////////////
	//
	// 	following is weifox's definitions of all components
	// 
	/////////////////////////////////////
	/**
	 * applications e.g. twitter sina fanfou digu... all app that have APIs
	 */
	apps:{},
	/**
	 * interact with browser
	 */
	browser:{},
	
	/**
	 * 网络通信 network functions 
	 */
	net:{},
	
	/**
	 * 界面
	 */
	ui:{},
	
	/**
	 * 数据存储
	 */
	data:{},
	
	/**
	 * error handle
	 * 
	 */
	error:{},
	/**
	 * 各种常用组件函数
	 */
	util:{},
	
	debug:{}
};
window.addEventListener('load',Weifox.init,false); 
window.addEventListener('unload',Weifox.unload,false);
